<%@page import="me.yobol.greentable.bean.CartCommodityBean"%>
<%@page import="me.yobol.greentable.service.CartCommodityService"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="me.yobol.greentable.commons.LoginStatus" %>
<%@ page import="me.yobol.greentable.commons.path.*" %>
<%@ page import="me.yobol.greentable.bean.IngredientBean" %>
<%@ page import="me.yobol.greentable.bean.IngredientCommentBean" %>
<%@page import="me.yobol.greentable.service.UserService"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  	<head>
    
    <title>Ingredient</title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<style>
		#comment_list{
			border:1px;
			border-style:solid;
		}
	</style>
  	</head>
  
	<body>
	<%
  		String msg = (String)request.getParameter("msg");
  		if(msg != null)
		{
			if(msg.equals("CartCommodityDuplication"))
			{
	%>
				<script>
					alert("该食材已经加入到购物车中，请到购物车中查看");
				</script>
	<%
			}
		}
	%>
	<%
		int loginStatus = (session.getAttribute("LoginStatus") == null) ? LoginStatus.ANONYMOUS : (Integer)session.getAttribute("LoginStatus");
		
		IngredientBean ig = (IngredientBean)request.getSession().getAttribute("ig");
		String traderName = (String)request.getSession().getAttribute("traderName");
		int igId = ig.getIngredientId();
		String igName = ig.getIngredientName();
		String igDescription = ig.getIngredientDescription();
		String igImg = ig.getIngredientImg();
		float igPrice = ig.getIngredientPrice();
		int igCommtentCtr = ig.getIngredientCommentCtr();
		int igFavouriteCtr = ig.getIngredientFavouriteCtr();
		int igScansCtr = ig.getIngredientScansCtr();
		
		List<IngredientCommentBean> comments = (List<IngredientCommentBean>)request.getSession().getAttribute("ic");
		int userId = (session.getAttribute("uId") == null) ? 0 : (Integer)session.getAttribute("uId");
		
	%>
		<h1><%=igName %></h1>
		<p><span style="color:#ccc;"><%=igDescription %></span> 商家:<%=traderName %></p>
		<p>浏览数:<%=igScansCtr %>&nbsp;收藏数:<%=igFavouriteCtr %>&nbsp;评论:<%=igCommtentCtr %></p>
		<p>
			<%=igPrice %>￥&nbsp;&nbsp;&nbsp;&nbsp;
			<%
				if(loginStatus == LoginStatus.CUSTOMER){
					if(CartCommodityService.getCCByIgIdAndCId(igId, userId) == null){
			%>
				<a href="<%=SPath.sCreateCartCommodity(request,"?igId=" + igId) %>">加入购物车</a>
			<%
					}else{
			%>
				<a href="<%=SPath.sGetCartCommodityList(request) %> ">已加入购物车，去我的购物车</a>
			<%
					}
				}
			%>
		</p>
		<img src="<%=igImg %>" />
		
		<!-- 评论 -->
		<div>
			<form id="comment" action="<%=SPath.sCreateIngredientComment(request) %>" method="post" onsubmit="return checkpost();">
			<%
				if(loginStatus == LoginStatus.ANONYMOUS){
			%>
				<p>你还没登录！请<a href="<%=PPath.pLogin(request) %>">登录</a>后评论。</p>
			<%
				}else{
			%>
				<textarea rows="10" cols="30" id="comment_content" name="commentContent" ></textarea>
				<input type="hidden" name="igId" value="<%=ig.getIngredientId() %>"/>
				<input type="submit" value="发表评论"/>
			<%
				}
			%>
			</form>
			<div>
				<%
					if(!comments.isEmpty()){
						for(IngredientCommentBean c : comments){
				%>
						<div id="comment_list">
							<p>
							<%=UserService.getUNameByUID(c.getUserId()) %>
							<%
								if(c.getUserId() == userId){
							%>
									&nbsp;&nbsp;<a href="<%=SPath.sDeleteIngredientComment(request,"?icId=" + c.getIngredientCommentId() + "&igId=" + ig.getIngredientId()) %>">删除</a>
							<%
								}
							%>
							</p>
							<p><%=c.getIngredientCommentContent() %></p>
						</div>
				<%
						}
					}
				%>
			</div>
		</div>
	
		<script type="text/javascript" src="<%=JPath.jJquery(request)%>"></script>
		<script type="text/javascript" src="<%=JPath.jComment(request) %>" charset="gb2312"></script>
  	</body>
</html>